<template>
  <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
   <!-- 卡片 -->
<el-card class="box-card">
  <el-row :gutter="30">
      <!-- 搜索 -->
    <el-col :span="8">
      <el-input placeholder="请输入内容" v-model="queryInfo.query" @keyup.enter.native="getGoodsList" clearable @clear="getGoodsList">
        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
      </el-input>
    </el-col>
    <el-col :span="4">
<!--      <el-button type="primary" @click="goAdd">-->
<!--        添加商品-->
<!--      </el-button>-->
    </el-col>
</el-row>
<!-- 表格 -->
<el-table :data="goodsList" style="width: 100%" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="name" label="商品名称" ></el-table-column>
      <el-table-column prop="price" label="商品价格" width="90"></el-table-column>
      <el-table-column prop="type" label="商品来源" width="70"></el-table-column>
      <el-table-column prop="allNumber" label="商品总数" width="70"></el-table-column>
      <el-table-column prop="dateCreated" label="创建时间" width="140">
        <template slot-scope='scope'>
          {{scope.row.dateCreated|dateFormat}}
        </template>
      </el-table-column>
      <el-table-column  label="操作" width="130">
        <template slot-scope="scope">
         <el-button type="primary" size="mini" @click="addToCar(scope.row)" class="el-icon-document-add"></el-button>
         <el-button type="danger" size="mini" @click="removeById(scope.row.goods_id)" class="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
<!--    <el-pagination-->
<!--        @size-change="handleSizeChange"-->
<!--        @current-change="handleCurrentChange"-->
<!--        :current-page="queryInfo.pagenum"-->
<!--        :page-sizes="[1, 2, 5, 10]"-->
<!--        :page-size="queryInfo.pageSize"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--        :total="total">-->
<!--      </el-pagination>-->
    </el-card>
  </div>
</template>
<script>
export default {
  data(){
    return {
      queryInfo:{
        query:'',
        pagenum:1,
        pagesize:10
      },
      goodsList:[],
      total:0
    }
  },
  created(){
    this.getGoodsList()
  },
  methods:{
    getGoodsList:async function(){
      const {data:res}=await this.$http.get('shop/findAllcomm',)
        if(res.data.status!==200){
        return this.$message.error("获取失败")
      }
      this.$message.success("获取商品列表成功")
      this.goodsList=res.meta
      console.log(res.meta)
      this.total=res.meta.length
    },
    // 获取最新的pagesize
    handleSizeChange:function(newsize){
        this.queryInfo.pagesize=newsize
        this.getGoodsList()
    },
    handleCurrentChange:function(newPage){
      this.queryInfo.pagenum=newPage
      this.getGoodsList()
    },
    removeById:async function(id){
      // 确认对话框
      const confirmResult=await this.$confirm("此操作将永久删除,是否继续?",'删除商品',{
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
      }).catch(err=>err)
      if(confirmResult!=='confirm'){
        return this.$message.info('已取消')
      }
      const {data:res}=await this.$http.delete(`goods/${id}`)
      console.log(res)
        if(res.data.status!==200){
        return this.$message.error("删除失败")
      }
      this.$message.success("删除成功")
      this.getGoodsList()
    },
    addToCar:async function(good) {
      console.log(good,'good')
      const { data: res } = await this.$http.get(`shop/addCommToCar?commNames=${good.name}&uid=${good.commId}`)
      console.log(res)
      if (res.data.status !== 200) {
        return this.$message.error("购买失败")
      }
      this.$message.success("购买成功!!")
    }
  }
}
</script>
<style lang="less">

</style>
